package com.newdjk.newdoctor.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.utils.LogUtils;


public class SwitchEnvironmentDialog {
    private Context mContext;
    private Dialog mDialog;

    public SwitchEnvironmentDialog(Context mContext) {
        this.mContext = mContext;
    }

    public void show( final DialogListener listener) {
        try {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);//dialog样式
            View view = View.inflate(mContext, R.layout.dialog_pick_env, null);
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setCancelable(false);
            mDialog.setContentView(view);

            TextView test = view.findViewById(R.id.env_test);
            TextView online = view.findViewById(R.id.env_publish);
            Button cancel = view.findViewById(R.id.bu_cancel);



            //测试环境
            test.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.switchTestEnv();
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                }
            });

            //正式环境
            online.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.switchOnlineEnv();
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                }
            });

            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                }
            });
            mDialog.show();
            Window dialogWindow = mDialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
            dialogWindow.setAttributes(lp);

        } catch (Exception e) {
            LogUtils.d("","LoadDialog  error!!!");
        }
    }

    private void close() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public interface DialogListener {

        void switchTestEnv();
        void switchOnlineEnv();
    }
}
